package starcoast.bocai.com.starcoast.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.common.BaseActivity;
import starcoast.bocai.com.starcoast.fragments.LoginCodeFragment;
import starcoast.bocai.com.starcoast.fragments.LoginPasswordFragment;

/**
 * Created by admin on 2016/6/3.
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.order_tab)
    TabLayout mTabLayout;
    @Bind(R.id.order_vp)
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initToolbar("星耀控股");
        mToolbar.inflateMenu(R.menu.menu_register);
        viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        viewPager.setOffscreenPageLimit(1);
        mTabLayout.setupWithViewPager(viewPager);
        init();
    }
    public void init(){
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                return false;
            }
        });
    }
    static class Adapter extends FragmentPagerAdapter
    {
        private final Fragment[] mFragments = new Fragment[2];
        private final String [] mFragmentTitles = {"密码登录","验证码登录"};

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {
            Fragment fragment = mFragments[position];
            if(position==0){

                if (fragment == null)
                {
                    fragment = new LoginPasswordFragment();
                    mFragments[position] = fragment;
                }
            }
            if(position==1){

                if (fragment == null)
                {
                    fragment = new LoginCodeFragment();
                    mFragments[position] = fragment;
                }
            }
            return fragment;
        }

        @Override
        public int getCount()
        {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position)
        {
            return mFragmentTitles[position];
        }
    }
}
